﻿using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Application.Contracts.TaskApp.Dtos;
using PLCS.Client.IService.LogManager;
using PLCS.Shared.Common.Constant;

using Prism.Services.Dialogs;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Volo.Abp.ObjectMapping;

namespace PLCS.App.OrderTask.ViewModels.Instruction
{
    public class InstructionDetailViewModel : HostDialogViewModel
    {
        private InstructionDto _model;
        private readonly IObjectMapper<OrderTaskModule> _objectMapper;
        private readonly IInstructionService _instructionService;
        private readonly IOperationLogService _logShowService;

        public InstructionDto Model
        {
            get { return _model; }
            set { SetProperty(ref _model, value); }
        }

        public InstructionDetailViewModel(IObjectMapper<OrderTaskModule> objectMapper, IInstructionService instructionService, IOperationLogService logShowService)
        {
            _objectMapper = objectMapper;
            _instructionService = instructionService;
            this._logShowService = logShowService;
            View = "指令管理弹窗";
        }

        protected override async void Save()
        {
            try
            {
                SavebuttonIsEnabled = false;
                var instruction = _objectMapper.Map<InstructionDto, InstructionEditDto>(Model);
                if (instruction.Id != Guid.Empty)
                {
                    await _instructionService.UpdateAsync(instruction);
                    Log("修改", instruction);
                }
                else
                {
                    await _instructionService.CreateAsync(instruction);
                    Log("新增", instruction);
                }

                ShowMessage("操作成功");
                base.Save();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
            finally { SavebuttonIsEnabled = true; }
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters.ContainsKey("Value"))
            {
                Model = parameters.GetValue<InstructionDto>("Value");
                Title = "编辑";
            }
            else
            {
                Title = "新增";
                Model = new InstructionDto();
            }
        }
    }
}